﻿@page "/leaderboard"
@inject BlazorBattles.Client.Services.ILeaderboardService LeaderboardService
@inject AuthenticationStateProvider AuthStateProvider
@inject BlazorBattles.Client.Services.IBattleService BattleService
@inject BlazorBattles.Client.Services.IBananaService BananaService
@inject IToastService ToastService
@inject NavigationManager NavigationManager

<h3>Leaderboard</h3>

@if (LeaderboardService.Leaderboard == null)
{
    <span>Loading Leaderboard...</span>
}
else
{
    <table class="table">
        <thead>
            <tr>
                <th>#</th>
                <th>User</th>
                <th>Victories</th>
                <th>Defeats</th>
                <th>Battles</th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            @foreach (BlazorBattles.Shared.UserStatistic entry in LeaderboardService.Leaderboard)
            {
                <tr style="@GetMyStyle(entry.UserId)">
                    <td>@entry.Rank</td>
                    <td>@entry.Username</td>
                    <td>@entry.Victories</td>
                    <td>@entry.Defeats</td>
                    <td>@entry.Battles</td>
                    <td>
                        @if (entry.UserId != myUserId)
                        {
                            <button class="btn btn-primary" @onclick="@(() => StartBattle(entry.UserId))">Fight</button>
                        }
                    </td>
                </tr>
            }
        </tbody>
    </table>
}

@code {

    int myUserId;

    protected override async Task OnInitializedAsync()
    {
        await LeaderboardService.GetLeaderboard();

        var authState = await AuthStateProvider.GetAuthenticationStateAsync();
        myUserId = int.Parse(authState.User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier).Value);
    }

    public string GetMyStyle(int userId)
    {
        if (userId == myUserId)
            return "color: green; font-weight: 600;";
        else
            return string.Empty;
    }

    public async Task StartBattle(int opponentId)
    {
        var result = await BattleService.StartBattle(opponentId);
        if (result.RoundsFought <= 0)
            ToastService.ShowInfo("The battle did not take place.");
        else if (result.IsVictory)
            ToastService.ShowSuccess("You won the battle!");
        else
            ToastService.ShowWarning("You have been defeated!");

        await LeaderboardService.GetLeaderboard();
        await BananaService.GetBananas();

        if (result.RoundsFought > 0)
            NavigationManager.NavigateTo("battlelog");
    }
}
